package com.ccp.dev.system.service;

import com.ccp.dev.core.util.AppUtil;
import com.ccp.dev.system.model.MessageLog;
import com.ccp.dev.system.model.MessageSend;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.mail.SimpleMailMessage;
import org.springframework.mail.javamail.JavaMailSender;
import org.springframework.stereotype.Component;

import java.util.List;

/**
 * ClassDescribe:
 * 消息引擎
 * @author :wangcheng
 * Date: 2019-08-07
 */
@Component
public class MessageEngine {
    private static final Logger LOGGER = LoggerFactory.getLogger(MessageEngine.class);

    //todo 多余参数确定不会用到后删除
    private JavaMailSender mailSender;

    private String fromUser = "";

    public void setFromUser(String fromUser) {
        this.fromUser = fromUser;
    }

    public void setJavaMailSender(JavaMailSender mailSender) {
        this.mailSender = mailSender;
    }

    /**
     * 发送邮件
     * @param msg 邮件对象
     */
    public void sendMail(SimpleMailMessage msg) {

    }

    /**
     * 发送手机短信
     * @param mobiles 手机号码列表
     * @param content 短信内容
     */
    public void sendSms(List<String> mobiles, String content) {

    }

    /**
     * 发送内部消息
     * @param messageSend 消息对象
     */
    public void sendInnerMessage(MessageSend messageSend) {
        Integer state = MessageLog.STATE_SUCCESS;
        try {
            MessageSendService messageSendService = AppUtil.getBean(MessageSendService.class);
            messageSendService.addMessageSend(messageSend);
        } catch (Exception e) {
            LOGGER.error(e.getMessage());
            state = MessageLog.STATE_FAIL;
            e.printStackTrace();
        }
        // 保存发送消息日志
        MessageLogService messageLogService = AppUtil.getBean(MessageLogService.class);
        messageLogService.addMessageLog(messageSend.getSubject(),messageSend.getReceiverName(),MessageLog.INNER_TYPE,
                state, messageSend.getTenantId());
    }
}
